<template>
  <view class="demo-container">
    <view class="header">
      <text class="title">人脸识别录像演示</text>
      <text class="subtitle">测试前置摄像头录像功能（3秒限制）</text>
    </view>
    
    <view class="demo-section">
      <text class="section-title">功能演示</text>
      <button class="demo-btn" @tap="openFaceCapture">
        打开录像界面
      </button>
    </view>
    
    <view class="demo-section">
      <text class="section-title">录制结果</text>
      <view v-if="capturedVideo" class="result-container">
        <video :src="capturedVideo" class="captured-video" controls></video>
        <text class="video-info">时长：{{ videoDuration }}秒</text>
        <button class="action-btn secondary" @tap="clearVideo">清除</button>
      </view>
      <view v-else class="no-result">
        <text class="no-result-text">暂无录制结果</text>
      </view>
    </view>
    
    <view class="info-section">
      <text class="info-title">使用说明</text>
      <view class="info-item">
        <text class="info-dot">•</text>
        <text class="info-text">点击"打开录像界面"进入拍摄页面</text>
      </view>
      <view class="info-item">
        <text class="info-dot">•</text>
        <text class="info-text">录制时长固定为3秒，会自动停止</text>
      </view>
      <view class="info-item">
        <text class="info-dot">•</text>
        <text class="info-text">录制完成后自动返回并显示结果</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      capturedVideo: '',
      videoDuration: 0
    }
  },
  
  onShow() {
    // 监听来自录像页面的结果
    uni.$on('faceCaptureResult', this.handleFaceCaptureResult);
  },
  
  onUnload() {
    uni.$off('faceCaptureResult', this.handleFaceCaptureResult);
  },
  
  methods: {
    openFaceCapture() {
      uni.navigateTo({
        url: '/pages/face-capture/face-capture'
      });
    },
    
    handleFaceCaptureResult(result) {
      if (result.success) {
        this.capturedVideo = result.videoPath;
        this.videoDuration = result.duration;
        
        uni.showToast({
          title: '录制成功',
          icon: 'success',
          duration: 2000
        });
      } else {
        uni.showToast({
          title: result.error || '录制失败',
          icon: 'none',
          duration: 2000
        });
      }
    },
    
    clearVideo() {
      this.capturedVideo = '';
      this.videoDuration = 0;
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-container {
  padding: 40rpx;
  min-height: 100vh;
  background: #f5f5f5;
}

.header {
  text-align: center;
  margin-bottom: 60rpx;
  
  .title {
    display: block;
    font-size: 40rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 16rpx;
  }
  
  .subtitle {
    font-size: 28rpx;
    color: #666;
  }
}

.demo-section {
  background: #fff;
  border-radius: 20rpx;
  padding: 40rpx;
  margin-bottom: 40rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  
  .section-title {
    display: block;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 30rpx;
  }
}

.demo-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 44rpx;
  font-size: 32rpx;
  font-weight: 500;
  transition: all 0.3s ease;
  
  &:active {
    transform: scale(0.98);
    opacity: 0.9;
  }
}

.result-container {
  text-align: center;
  
  .captured-video {
    width: 100%;
    height: 400rpx;
    border-radius: 12rpx;
    margin-bottom: 20rpx;
  }
  
  .video-info {
    display: block;
    font-size: 28rpx;
    color: #666;
    margin-bottom: 30rpx;
  }
}

.action-btn {
  height: 72rpx;
  padding: 0 40rpx;
  border-radius: 36rpx;
  font-size: 28rpx;
  
  &.secondary {
    background: #f0f0f0;
    color: #666;
    border: 1rpx solid #ddd;
  }
}

.no-result {
  text-align: center;
  padding: 80rpx 0;
  
  .no-result-text {
    font-size: 28rpx;
    color: #999;
  }
}

.info-section {
  background: #fff;
  border-radius: 20rpx;
  padding: 40rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  
  .info-title {
    display: block;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 30rpx;
  }
  
  .info-item {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .info-dot {
      color: #667eea;
      margin-right: 20rpx;
      font-size: 32rpx;
    }
    
    .info-text {
      font-size: 28rpx;
      color: #666;
      flex: 1;
    }
  }
}
</style>